package org.zyj.io.protocal.length_field_based_frame;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.zyj.io.protocal.Message;

public class LengthFieldBasedFrameDecoderInitializer extends ChannelInitializer<NioSocketChannel> {

    @Override
    protected void initChannel(NioSocketChannel ch) throws Exception {
        ch.pipeline() //配置pipeline
                .addLast(new LengthFieldBasedFrameMessageDecoder())
                .addLast(new ServerHandler())
        ;
    }

    static class ServerHandler extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            Message m = (Message) msg;
            System.out.println(m);
            System.out.println(new String(m.getBody())); //暂时不管字符串编码的问题
        }
    }
}
